<?php
/**
 * Created by PhpStorm.
 * User: user
 * Date: 2016/4/7
 * Time: 18:54
 */

namespace front\controllers;


use EasyWeChat\User\User;
use EasyWeChat\Foundation\Application;
use EasyWeChat\Payment\Order;
use EasyWeChat\Support\Url as UrlHelper;
use EasyWeChat\Support\Url;
use GuzzleHttp\Client;
use order\models\Orders;
use users\modules\region\models\Region;
use users\services\RegionService;
use Yii;
use yii\base\Exception;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
use yii\web\Controller;
use yii\web\Cookie;

class TestController extends Controller
{
    function init() {
        parent::init();
    }
    public function actionIndex() {
        $pro = Region::find()->select(['id', 'name'])->where(['level'=>1])->asArray()->all();
        $arr = [];
        foreach($pro as $value) {
            $pid = substr($value['id'], 0, 2);
            $province = [];
            $province['areaId'] = $pid;
            $province['areaName'] = $value['name'];
            $city = Region::find()->select(['id', 'name'])->where(['level' => 2, 'parent_id' => $value['id']])->asArray()->all();
            foreach ($city as $val) {
                $id = substr($val['id'], 0, 4);
                $city = [];
                $city['areaId'] = $id;
                $city['areaName'] = $val['name'];
                $dist = Region::find()->select(['id', 'name'])->where(['level' => 3, 'parent_id' => $val['id']])->asArray()->all();
                foreach ($dist as $v) {
                    $district = [];
                    $district['areaId'] = $v['id'];
                    $district['areaName'] = $v['name'];
                    $city['counties'][] = $district;
                }
                $province['cities'][] = $city;
            }
            $arr[] = $province;
        }
        print_r(Json::encode($arr));
    }

    public function actionFather() {

        /*
         * 使用微信接口获取用户经纬度
         */
        // $configForPickLag = \Yii::$app->request->cookies->getValue('configForPickLag');
        if(empty($configForPickLag)){
            $app = new Application([
                'app_id'  => 'wxb92c2bb9c487c950',         // AppID
                'secret'  => '4531c2e3fcf9438320af5cb85b068549',     // AppSecret
                'payment' => [
                    'merchant_id'        => '1301495501',
                    'key'                => 'Yk3Bn8sBsg2yJ83gfkKHgfehe7Hdggsk',
                ]
            ]);

            if (empty($_GET['code'])) {
                $currentUrl = Url::current();  // 获取当前页 URL
                $response = $app->oauth->scopes(['snsapi_base'])->redirect($currentUrl);
                $response->send(); // or echo $response;
            }
            // 授权回来
            /** @var User $user */
            $user = $app->oauth->user();
            $token = $user->getAccessToken();

            //获取accsee_token
            $access_token = $token->getToken();

            //
            print_r($access_token);
            $payment = $app->payment;
            $configForPickAddress = $payment->configForGetLocation($token) ? $payment->configForGetLocation($token) : 0;

            $cookies = \Yii::$app->response->cookies;
            $cookies->add(new Cookie([
                'name' => 'configForPickLag',
                'value' => $configForPickAddress,
            ]));
        }
        return  $this->render('father',[
            'parameters'=>$configForPickAddress,
        ]);
    }
    public function actionAddress() {
        $cache = Yii::$app->cache->get(11);
        $order = Orders::findOne(['order_id' => 632]);
        $order = ArrayHelper::toArray($order);
        xdebug_var_dump($order);exit;
        // 定义参数
        return  $this->render('address',[
        ]);
    }
}